#!/bin/bash
# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: jaewon

set -e
export TZ="UTC"

RECIPIENT="jaewon@prestolabs.io"
VENV="/remote/iosg/home/jaewon/venv"
TODAY="$(date '+%Y%m%d')"
LOG_FILE="/remote/iosg/home/jaewon/ops/bitmex/log/log-${TODAY}"

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
cd "${SCRIPT_DIR}"
GIT_ROOT="$(git rev-parse --show-toplevel)"
cd "${GIT_ROOT}/python"

source "${VENV}/bin/activate"
export PYTHONPATH="${GIT_ROOT}/python"

set +e
luigi --local-scheduler \
      --log-level INFO \
      --retcode-task-failed 2 \
      --module ops.bitmex_param_autopick.luigi \
      DailyTask \
      &> "${LOG_FILE}"
RES="${?}"

if [ ${RES} -eq 0 ]; then
  RESULT="SUCCESS"
else
  RESULT="FAIL"
fi

./pyrunner coin/tool/mailer.py \
    --mailfrom='bitmex-param-autopick@joomo.io' \
    --mailto="${RECIPIENT}" \
    --subject="[${RESULT}] Bitmex parameter auto-pick ${TODAY}" < "${LOG_FILE}"
